package com.abel.roommanager.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import com.abel.roommanager.pojo.Room;
import com.abel.roommanager.provider.RoomFindSql;
import com.abel.roommanager.provider.RoomsCountSql;

public interface RoomMapper {
	@Select("select * from room where id=#{id}")
	Room findRoomById(int id);
   @Update("update room set livenumber=livenumber+#{num} where id=#{roomno}")
	int updateLiveNumberByRoomno(@Param("roomno")int roomno,@Param("num")int num);
   @Select("select id,address from room where livenumber<capacity")
     List<Room> findAllRoom();
   @Select("select * from room where livenumber<capacity and type=#{sex}")
     List<Room> findRoomsByType(int sex);
   @Select("select * from room limit #{index},#{count}")
   @Results({
	   @Result(id=true,column="id",property="id"),
	   @Result(column="address",property="address"),
	   @Result(column="capacity",property="capacity"),
	   @Result(column="livenumber",property="livenumber"),
	   @Result(column="rent",property="rent"),
	   @Result(column="state",property="state"),
	   @Result(column="payway",property="payway"),
	   @Result(column="type",property="type"),
	   @Result(column="renttime",property="renttime"),
	   @Result(column="lid",property="landlord",one=@One(
			   select="com.abel.roommanager.mapper.LandlordMapper.findLandlordByLid"
			   ))
   })
     List<Room> findRoomsByCurPage(@Param("index")int index, @Param("count")int count);
   @Select("select count(*) from room") 
   int findCountRooms();
   @Insert("insert into room values(default,#{address},#{capacity},default,#{rent},#{state},#{payway},#{type},#{renttime},#{lid},#{rtid})")
    int addRoom(Room room);
   @Delete("delete from room where id=#{id}")
     int delRoom(int id);
   @Select("select * from room where id=#{id}")
   @Results({
	   @Result(id=true,column="id",property="id"),
	   @Result(column="address",property="address"),
	   @Result(column="capacity",property="capacity"),
	   @Result(column="livenumber",property="livenumber"),
	   @Result(column="rent",property="rent"),
	   @Result(column="payway",property="payway"),
	   @Result(column="state",property="state"),
	   @Result(column="type",property="type"),
	   @Result(column="renttime",property="renttime"),
	   @Result(column="id",property="students",many=@Many(
			   select="com.abel.roommanager.mapper.StudentMapper.findStudentsByRid"
			   )),
	   @Result(column="lid",property="landlord",one=@One(
			   select="com.abel.roommanager.mapper.LandlordMapper.findLandlordByLid"
			   )),
	   @Result(column="rtid",property="roomType",one=@One(
			   select="com.abel.roommanager.mapper.RoomTypeMapper.findRoomTypeById"
			   ))
   })
   Room findRoomInfoById(int id);
   @SelectProvider(type=RoomFindSql.class,method="getSql")
   @Results({
	   @Result(id=true,column="id",property="id"),
	   @Result(column="address",property="address"),
	   @Result(column="capacity",property="capacity"),
	   @Result(column="livenumber",property="livenumber"),
	   @Result(column="rent",property="rent"),
	   @Result(column="state",property="state"),
	   @Result(column="payway",property="payway"),
	   @Result(column="type",property="type"),
	   @Result(column="renttime",property="renttime"),
	   @Result(column="lid",property="landlord",one=@One(
			   select="com.abel.roommanager.mapper.LandlordMapper.findLandlordByLid"
			   ))
   })
   List<Room> findRoomsByLimits(@Param("room")Room room, @Param("enableLiving")int enableLiving, @Param("index")int index, @Param("count")int count);
   @SelectProvider(type=RoomsCountSql.class,method="getSql")
   int findCountRoomByLimits(@Param("room")Room room, @Param("enableLiving")int enableLiving);
   @Select("select id,address from room where type=#{sex} and livenumber<capacity")
   List<Room> findRoomsBySex(int sex);
   @Select("select id,address from room")
   List<Room> findRooms();
}
